package com.hao.shiro.stared;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

/**
 * TODO
 *
 * @author 97472
 * @version 1.0
 * @date 2021/8/28 13:38
 */
public class shiroTest {
    @Test
    public void shiroTest(){
//        1，初始化shiro的安全管理器
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
//        2,设置用户的权限到安全管理器
        IniRealm realm = new IniRealm("classpath:com.hao.shiro.ini");
        securityManager.setRealm(realm);

//        3,使用SecurityUtils将securityManager设置到运行的环境中
        SecurityUtils.setSecurityManager(securityManager);
//        4,从SecurityUtils中获取主题subject
        Subject subject = SecurityUtils.getSubject();
//        5,创建一个token令牌，记录了用户认证的凭证账号和密码
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("lisi","123123");
//        6,进行登录认证操作
        subject.login(usernamePasswordToken);
//        7,判断用户是否登录
        System.out.println("是否登录？"+subject.isAuthenticated());
        subject.logout();
        System.out.println("是否登录？"+subject.isAuthenticated());
    }
}
